package foodserver;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.subway.babynote.syncdata.ClientDataRecord;
import com.subway.babynote.syncdata.PostParam;
import com.subway.babynote.syncdata.errMessage;


import querysrc.query_addweibo;

/**
 * Servlet implementation class syncweibo
 */
public class syncweibo extends addbase {
	private static final long serialVersionUID = 1L;
     query_addweibo qa = null; 
    /**
     * @see addbase#addbase()
     */
    public syncweibo() {
        super();
        // TODO Auto-generated constructor stub
    }
    @Override
	protected JSONObject read_data(HttpServletRequest request) 
	{
		JSONObject jsono = new JSONObject();
		try
		{
			String uai_str = request.getParameter(PostParam.user_auth_info_Param.record);
			String feed_str = request.getParameter(PostParam.last_avgRecord_Param.records);
			JSONObject uaijson = new JSONObject(uai_str);
			JSONArray feedjson = new JSONArray(feed_str);
			jsono.put(PostParam.user_auth_info_Param.record, uaijson);
			jsono.put(PostParam.last_avgRecord_Param.records, feedjson);
		}
		catch(Exception ex)
		{
			
		}		
		// TODO Auto-generated method stub
		return jsono;
	}	
	@Override
	protected int add_data(JSONObject input_jsono) {
		try
		 {		
			 int iret = 0;		

			 JSONObject jsono_uai = input_jsono.getJSONObject(PostParam.user_auth_info_Param.record);
			 
			 ClientDataRecord.user_auth_info uai = new ClientDataRecord.user_auth_info(jsono_uai);
			 
			 boolean bret = this.qau.sync_authen(uai.id, uai.imsi, uai.imei);
			 
			 if(bret)
			 {
				 JSONArray jsona = input_jsono.getJSONArray(PostParam.last_avgRecord_Param.records);
				 int record_count = jsona.length();
				 ArrayList<ClientDataRecord.weiboRecord> records=new ArrayList<ClientDataRecord.weiboRecord>(record_count);
				 
				 for(int i=0;i<record_count;i++)
				 {
					 JSONObject tempjsono = jsona.getJSONObject(i);
					 
					 ClientDataRecord.weiboRecord fr = new ClientDataRecord.weiboRecord(tempjsono);					 					 
					 records.add(fr);
					 			 
				 }			 
				 
				 iret = qa.addWeibo(records, uai.id);
			 }
			 else
				 iret = errMessage.err_authenfail.errcode;
			 return iret;
		 }
		 catch (JSONException e) {
				// TODO Auto-generated catch block
				System.out.println("JSON error for FeedSync doPost:" + e.getMessage());
				e.printStackTrace();
				return -1;
				
			}
		 catch (Exception e) {
			 System.out.println("FeedSync doPost error:" + e.getMessage());
		      e.printStackTrace();
		      return -1;
		    }
	}

	@Override
	protected void initialize_subquery() {
		// TODO Auto-generated method stub
		qa = new query_addweibo(this.sqlconhandler);
	}

	@Override
	protected void destory_subquery() {
		// TODO Auto-generated method stub
		this.release_query(qa);
	}



}
